﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Sunny.UI;

namespace SmartMotion.Core.Tools
{
    [ReflectAble]
    public  class IfItem:ActionExpression
    {
        private int _currentIndex;

        [Browsable(false),JsonIgnore]
        public bool Completed { get; set; }
        public override UIPage GetParmsPage()
        {
            return null;    
        }

        protected override void NextActionItemIndex(bool result)
        {

        
            {
                int sum = 0;//用来记录嵌套次数
                //查找所有分支，赋值MyIf
                for (int i = CurrentIndex + 1; i < this.Flow.ActionItems.Count - 1; i++)
                {
                    ActionItem nextItem = this.Flow.ActionItems[i];
                    //查找If嵌套
                    if (nextItem.GetType() == typeof(IfItem))
                    {
                        sum += 1; //嵌套+1
                    }
                    if (nextItem.GetType() == typeof(EndIfItem))
                    {
                        if (sum == 0)
                        {
                            break;//找到Endif就没必要继续搜索下去了
                        }
                        sum -= 1;//嵌套-1
                    }

                    if (sum == 0)//如果没有嵌套
                    {
                        if (nextItem is ElseIfItem elseifItem)
                        {
                            elseifItem.IfItem = this;
                        }
                        if (nextItem is ElseItem elseItem)
                        {
                            elseItem.IfItem = this;
                        }
                    }
                }
            }
            if (result)
            {
                Flow.NextIndex++;
                Completed = true;
            }
            else
            {
                Completed = false;
                int sum = 0;//用来记录嵌套次数
                            //查找ElseIF
                for (int i = CurrentIndex + 1; i < this.Flow.ActionItems.Count; i++)
                {
                    ActionItem nextItem = this.Flow.ActionItems[i];
                    //查找If嵌套
                    if (nextItem.GetType() == typeof(IfItem))
                    {
                        sum += 1; //嵌套+1
                    }
                    if (nextItem.GetType() == typeof(EndIfItem))
                    {
                        if (sum == 0)
                        {
                            if (nextItem.GetType() == typeof(EndIfItem))
                            {
                                this.Flow.NextIndex = i;
                                return;
                            }
                        }
                        sum -= 1;//嵌套-1
                    }

                    if (sum == 0)//如果没有嵌套
                    {
                        if (nextItem.GetType() == typeof(ElseIfItem))
                        {
                            this.Flow.NextIndex = i;
                            return;
                        }

                        if (nextItem.GetType() == typeof(ElseItem))
                        {
                            this.Flow.NextIndex = i;
                            return;
                        }

                    }
                }
            }


        }

       
    }


    [ReflectAble]
    public class ElseIfItem : ActionExpression
    {
        [Browsable(false),JsonIgnore]
        public IfItem IfItem { get; set; }
    }
    [ReflectAble]
    public class ElseItem : ActionItem  
    {
        [Browsable(false), JsonIgnore]
        public IfItem IfItem { get; set; }
        public override UIPage GetParmsPage()
        {   
            return null;
        }

        protected override bool RunMethod()
        {
            return true;
        }

        protected override void NextActionItemIndex(bool result)
        {
            if (IfItem.Completed)//如果已经执行过一个分支,就跳到EndIf
            {
                int sum = 0;//用来记录嵌套次数
                //查找ElseIF
                for (int i = CurrentIndex + 1; i < Flow.ActionItems.Count; i++)
                {
                    ActionItem nextItem = Flow.ActionItems[i];
                    //查找If嵌套
                    if (nextItem.GetType() == typeof(IfItem))
                    {
                        sum += 1; //嵌套+1
                    }
                    if (nextItem.GetType() == typeof(EndIfItem))
                    {
                        if (sum == 0)//如果没有嵌套
                        {
                            Flow.NextIndex = i;//EndIf
                            return;
                        }
                        sum -= 1;//嵌套-1
                    }
                }
            }
            else
            {
                Flow.NextIndex++;//如果没有进入过某个分支，就执行Else 的下一句
            }
        }
    }
    [ReflectAble]
    public class EndIfItem : ActionItem
    {
        [Browsable(false), JsonIgnore]
        public IfItem IfItem { get; set; }
        public override UIPage GetParmsPage()
        {
            return null;
        }

        protected override bool RunMethod()
        {
            return true;
        }

    }

}
